import { boss_bullet_list } from "./canvas.js";
import {get_deg_location,mydeg} from './tool.js'
let user_plane=null
export function init_user_plane(user_plane){
  user_plane=user_plane
}
export let show_boss=false
export function crate_boss(){
  show_boss=true
}
var max_width = document.body.offsetWidth;
var max_height = document.documentElement.clientHeight;
export function boss (user_plane){
  
  this.life = 888
  this.random_atk = 0
  this.time = null
  this.width = 300
  this.height = 186
  this.top = 0
  this.left = max_width / 2 - this.width/2
  this.dom=null
  this.random_atk=null
  this.create =()=> {
    console.log('boss生成')
    this.dom = document.getElementById("women_boss");
    let befor_atk = 0;
    this.time = setInterval(() => {
      this.random_atk = Math.ceil(Math.random() * 5);
      
      // if (random_atk == befor_atk) {
      //   return;
      // } else {
      //   befor_atk = random_atk;
      // }
      // random_atk = 6;
      console.log(this.random_atk)
      switch (this.random_atk) {
        case 1:
          boss_atk1(this,user_plane);
          break;
        case 2:
          boss_atk2(this);
          break;
        case 3:
          boss_atk3();
          break;
        case 4:
          boss_atk4(this);
          break;
        case 5:
          boss_atk4(this);
          break;
        
      }
    }, 4000);
  },
  this.die=()=>{
    
  }
}


function boss_atk1_bullet() {
    this.width = 57;
    this.height = 99;
    this.top = null;
    this.left = null;
    this.speed = 10;
    this.deg = 0;
    this.xspeed = null;
    this.yspeed = null;
    this.atk=5
    this.create = (x, y, xs, ys) => {
      console.log(x, y, xs, ys);
      this.dom = document.getElementById("boss_atk2");
      this.left = x;
      this.top = y;
      this.xspeed = xs;
      this.yspeed = ys;
      this.move();
    };
    this.move = () => {
      let tep = 0;
      this.time = setInterval(() => {
        this.top += this.yspeed;
        this.left += this.xspeed - 3;
        if (tep >= 10) {
          this.width += 20;
          this.height += 20;
          tep = 0;
        }
        tep++;
      }, 20);
    };
  }
//太阳
function boss_atk2_bullet() {
  this.width = 40;
  this.height = 40;
  this.top = null;
  this.left = null;
  this.speed = 10;
  this.deg = 0;
  this.atk=2
  this.xspeed = null;
  this.yspeed = null;
  this.create = (x, y, xs, ys) => {
    this.dom = document.getElementById("boss_atk2");
    this.left = x
    this.top = y
    this.xspeed = xs;
    this.yspeed = ys;
    this.move();
  };
  this.move = () => {
    this.time = setInterval(() => {
      this.top += this.yspeed;
      this.left += this.xspeed;
    }, 20);
  };
}
//激光
function boss_atk3_bullet() {
  this.width = 40;
  this.height = 72;
  this.top = null;
  this.left = null;
  this.speed = 10;
  this.deg = 0;
  this.atk=4
  this.xspeed = null;
  this.yspeed = null;
  this.create = (x, y, xs, ys) => {
    this.dom = document.getElementById("boss_atk3");
    this.left = x
    this.top = y;
    this.xspeed = xs;
    this.yspeed = ys;
    this.move();
  };
  this.move = () => {
    let power=0
    this.time = setInterval(() => {
      this.height=power*10+72
      if(this.height>max_height){
        this.left=-300
        
        clearInterval(this.time)
      }
      power+=1
    }, 20);
    
  };
}
//喷火
function boss_atk1(boss,user_plane) {
  let flag=0
  let time=setInterval(()=>{
    var boss_atk1_bullet1 = new boss_atk1_bullet();
    let x =
    user_plane.left +
    user_plane.width / 2 -
    (boss.left + boss.width / 2);
  let y = user_plane.top - (boss.top + boss.height);
  let distance = Math.sqrt(x * x + y * y);
  x = (x / distance) * 15;
  y = (y / distance) * 15;
  var boss_atk1_bullet1 = new boss_atk1_bullet();
  boss_atk1_bullet1.create(
    boss.left + boss.width / 2 - boss_atk1_bullet1.width / 2,
    boss.top + boss.height,
    x,
    y
  );
  boss_bullet_list.push(boss_atk1_bullet1);
  if(flag>=30){
    clearInterval(time)
  }
  flag++
  },50)
  
}
//向下散弹
function boss_atk2(boss) {
  let flag=1
  let time=setInterval(()=>{
    if(flag>=6){
      clearInterval(time)
    }
    for(let i=0;i<=12;i++){
      let deg=mydeg(15,i*30+15*(flag%2))
      var bullet_item=new boss_atk2_bullet()
      boss_bullet_list.push(bullet_item);
      bullet_item.create(
      boss.left + boss.width / 2,
      boss.top + boss.height,
      deg[0],deg[1]
      );
    }
    flag++
  },500) 
}
//激光雨幕
function boss_atk3(){
  for(let i =0;i<=max_width;i+=120){
    
      let bullet_item=new boss_atk3_bullet()
      bullet_item.create(i,0,0,0)
      boss_bullet_list.push(bullet_item)
    
  }  
}
//旋转攻击
function boss_atk4(boss){
  let flag=0
  let time=setInterval(()=>{
    let degs=mydeg(40,flag*45)
    var bullet_item = new boss_atk2_bullet();
    bullet_item.create(
    boss.left + boss.width / 2,
    boss.top + boss.height+flag*10,
    degs[0]/2,
    degs[1]/2
    );
    boss_bullet_list.push(bullet_item);
    flag++
    if(flag>=40){
      clearInterval(time)
    }
  },100)
}